<?php 
namespace Admin\Controller;


//公共的控制器类
class ScoreController extends AdminController
{
	private $_score = null; //积分表操作类

	//初始化
	public function _initialize()
	{
		parent::_initialize();
		$this->_score = D('score');
		$this->_user = M('user');

	}

	public function index()
	{   
		$search = I('get.phone');

		// $result = M()->table(array('表1'=>'表1缩写','表2'=>'表2缩写'))->field('表1缩写.字段1,表2缩写.字段2')->where('表2.wb_id=表1.id')->select();
		if(empty($search)) {

			$count = M()->table(array('lamp_score'=>'s','lamp_user'=>'u'))->field('s.time,s.id,s.score,u.name,u.phone')->where('s.uid=u.id')->count();
			// var_dump($count);exit;

			$Page = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)

			$show = $Page->show();// 分页显示输出
			$list = M()->table(array('lamp_score'=>'s','lamp_user'=>'u'))->field('s.time,s.id,s.score,u.name,u.phone')->limit($Page->firstRow.','.$Page->listRows)->where('s.uid=u.id')->select();


		    // $sql = "select s.id,s.score,s.status,u.name,u.phone from lamp_user u join lamp_score s on s.uid=u.id";
			// $list = M()->query($sql);
			// var_dump($list);exit;
			//分配变量
			$this->assign("list",$list);
			$this->assign("page",$show);

			// var_dump($list);exit;
			//加载模板	
			$this->display();
			exit;
		}

		// $data = M()->table('lamp_user  as  u')->join('lamp_score  as  s  on  s.uid = u.id')->where('name like "%'.$search.'%"')->select();

		// $result = M()->table(array('表1'=>'表1缩写','表2'=>'表2缩写'))->field('表1缩写.字段1,表2缩写.字段2')->where('表2.wb_id=表1.id')->select();

		$count = M()->table(array('lamp_score'=>'s','lamp_user'=>'u'))->field('s.time,s.id,s.score,u.name,u.phone')->where('s.uid=u.id and u.phone like "%'.$search.'%"')->count();
		// var_dump($count);exit;
		$Page = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$map['phone'] = $_GET['phone'];
		// var_dump($map);
		$Page->parameter=$map;
		$show = $Page->show();// 分页显示输出

		$list = M()->table(array('lamp_score'=>'s','lamp_user'=>'u'))->field('s.time,s.id,s.score,u.name,u.phone')->limit($Page->firstRow.','.$Page->listRows)->where('s.uid=u.id and u.phone like "%'.$search.'%"')->select();
		// var_dump($list);exit;
		//分配变量
		$this->assign("list",$list);
		$this->assign("page",$show);
		//加载模板	
		$this->display();
	   
	}

	//执行添加积分操作
	public function doadd()
	{
		// var_dump($this->_score);exit;
		if(!$this->_score->create()){
			$this->error($this->_score->getError());
			exit;
		}

		if($this->_score->add() > 0){
			$this->success("添加成功！",U('Score/index'));
		}else{
			$this->error("添加失败！");
		}
	}

	//加载修改页面
	public function edit()
	{
		//查出数据
		$sql = "select s.id,s.score,s.uid,u.name,u.phone from lamp_user u join lamp_score s on s.uid=u.id where s.id=".I('id');
		$vo = M()->query($sql);
		// var_dump($vo);exit;

		//向模板分配数据
		$this->assign('list',$vo);
		//加载模板
		$this->display();

	}
	//执行修改操作
	public function save()
	{
		// var_dump($this->_score->create());exit;
		//初始化
		if(!$this->_score->create()){
			$this->error($this->_score->getError());
			exit;
		}
		// var_dump($this->_score->fetchSql()->save());exit;
		//执行修改 
		if($this->_score->save() >= 0){
			$this->success("修改成功！",U('Score/index'));
		}else{
			$this->error("修改失败");
		}
	}

	//删除操作
	public function del(){
		//把用户角色表中相关的也删除
		if($this->_score->delete($_GET['id'])>0){
			$this->success("删除成功！",U('Score/index'));
		}else{
			$this->error("删除失败");
		}
	}


	public function doajax()
	{
		$id=I('post.id');
		$status = $this->_score->where(array('id'=>array('eq',$id)))->find();
		if($status['status']==0) {
			$status['status'] = 1;
			$list = $this->_score->where(array('id'=>array('eq',$id)))->save($status);
			echo $str = '未营业';
		} else {
			$status['status'] = 0;
		$list = $this->_score->where(array('id'=>array('eq',$id)))->save($status);

			echo $str = '营业中';
		}

	}







}
